package org.dx.oho.controller.shopadmin;

import org.dx.oho.dto.ShopExecution;
import org.dx.oho.entity.Area;
import org.dx.oho.entity.PersonInfo;
import org.dx.oho.entity.Shop;
import org.dx.oho.entity.ShopCategory;
import org.dx.oho.enums.ShopStateEnum;
import org.dx.oho.exception.ShopOperationException;
import org.dx.oho.service.ShopService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * create by duanx 2021/3/21
 *@description： 店铺管理
 *@classname: ShopAdminController
 *@author: duanx
 *@date: 2021/3/21 20:26
 **/
@Controller
@RequestMapping(value ="shopadmin" ,method = {RequestMethod.GET})
public class ShopAdminController {

    private final ShopService shopService;

    Logger logger = LoggerFactory.getLogger(ShopAdminController.class);

    public ShopAdminController(ShopService shopService) {
        this.shopService = shopService;
    }

    @RequestMapping(value = "/shopoperation")
    @ResponseBody
    @Transactional(rollbackFor = ShopOperationException.class)
    public Map<String ,Object> shopOperation(){
        System.out.println("调试信息");
        Map<String ,Object> map = new HashMap<>();
        Shop shop = new Shop();
        PersonInfo personInfo = new PersonInfo();
        Area area = new Area();
        ShopCategory shopCategory = new ShopCategory();
        personInfo.setUserId(8L);
        area.setAreaId(17);
        shopCategory.setShopCategoryId(12L);
        shop.setPersonInfo(personInfo);
        shop.setArea(area);
        shop.setShopCategory(shopCategory);
        shop.setShopName("蜜雪冰城");
        shop.setShopDesc("不好喝的奶茶");
        shop.setShopAddr("19窗口");
        shop.setPhone("15527112902");
        shop.setPriority(7);
        shop.setShopImg("");
        shop.setEnableStatus(ShopStateEnum.CHECK.getState());
        shop.setAdvice(ShopStateEnum.CHECK.getStateInfo());
        logger.info("店铺：==>"+shop.toString());
        logger.info("插入店铺：");
        File file = new File("E:/data/鄂尔多斯/常用文件/图片/19440eb74f8b4dd55b3018a0f6688f8.jpg");
        try {
            InputStream inputStream = new FileInputStream(file);
            ShopExecution shopExecution = shopService.addShop(shop,inputStream,file.getName() );
            map.put("shopExecution",shopExecution);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            map.put("shopExecution","ERROR");
            throw new ShopOperationException("创建店铺失败，请检查重试");
        }
        Shop shopFind = new Shop();
        shopFind.setPhone("15527112902");
        List<Shop> shopList = shopService.getShop(shopFind);
        map.put("row",shopList.size());
        map.put("shopList",shopList);
        return map;
    }
}
